//
//  WYDismantlingController.m
//  Boss
//
//  Created by admin on 2018/6/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYDismantlingController.h"
#import "WYRedPacketMessageController.h"
#import "UIImageView+WebCache.h"
#import "WYNearRedPacket.h"
#import <AVFoundation/AVFoundation.h>
@interface WYDismantlingController ()
@property (strong, nonatomic) IBOutlet UIButton *imageBtn;
@property (strong, nonatomic) IBOutlet UIImageView *userImageView;
@property (nonatomic,strong)WYNearRedPacket *redPacket;

@property (strong, nonatomic) IBOutlet UIButton *openButton;

@property (strong, nonatomic) IBOutlet UILabel *contentLabel;
@property (strong, nonatomic) IBOutlet UILabel *userName;


@end

@implementation WYDismantlingController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.userImageView setBorderWithWidth:kLineHeight*4 andColor:[UIColor hexStringToColor:@"#ffffff" alpha:1.0]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor hexStringToColor:@"#000000" alpha:0.5];
    self.view.backgroundColor = [UIColor clearColor];
    // Do any additional setup after loading the view from its nib.
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:self.redMessage.userPhoto] placeholderImage:[UIImage imageNamed:@"logo_place"]]; 
    [self.userImageView setCornerRadius:33.f];
    if (self.isExclusive) {
        self.contentLabel.text = @"恭喜你收到一个专属红包";
    }else{
        self.contentLabel.text = self.redPacket.redpacketName ;
    }
}
-(void)setIsExclusive:(BOOL)isExclusive
{
    _isExclusive = isExclusive;
    if (isExclusive) {
        self.contentLabel.text = @"恭喜你收到一个专属红包";
        self.userImageView.image = [UIImage imageNamed:@"newLogo"];
    }else{
        self.contentLabel.text = self.redPacket.redpacketName;
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setNameString:(NSString *)nameString
{
    _nameString = nameString;
    self.userName.text = nameString;
}
-(void)setContentString:(NSString *)contentString
{
    _contentString = contentString;
//    contentString = @"李江彪大傻逼 测试行间距 这是两行数字 应该差不多了 一行显示不了吧 行数的大于2行 还不能超过三行  傻逼李江彪想看看 这是不是差不多了";
//    self.contentLabel.attributedText = [contentString getAttributedStringWithLineSpace:3 kern:0];
    self.contentLabel.text = contentString;
}
-(void)setISVip:(BOOL)ISVip
{
    _ISVip = ISVip;
    self.vipImageView.hidden = ISVip;
}
-(void)setImageView:(NSString *)imageView
{
    _imageView = imageView;
    self.vipImageView.image = [UIImage imageNamed:imageView];
}
- (IBAction)exitAction:(id)sender {
    if (self.exit) {
        self.exit();
    }
    
    
  //  self.tabBarController.tabBar.hidden  = NO;
    [self dismissViewControllerAnimated:YES completion:^{
      //  self.tabBarController.tabBar.hidden = NO;
    }];
     self.presentingViewController.tabBarController.tabBar.hidden = NO; 
}

- (IBAction)openAction:(id)sender {
    WEAKSELF
    if (self.isExclusive) {
        
        [WYHttpServer receiveSharePacketWithPacketId:self.model.id SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
            
            if (success) {
                if (weakSelf.complte) {
                    [weakSelf playerVideo];
                    weakSelf.complte();
                    
                }
            }else{
                if (weakSelf.errorComplte) {
                    weakSelf.errorComplte(errorMessage);
                }
            }
            
            
            
        }];
    }else{
        
        NSArray *coorArray = [self.redMessage.coordinate componentsSeparatedByString:@","];
        
        self.coordinate = [NSString stringWithFormat:@"%@,%@",coorArray[0],coorArray[1]];
        self.redpacketId = self.redMessage.redpacketId;
        self.redpacketType = self.redMessage.redpacketType;

        CABasicAnimation* rotationAnimation;
        
        //绕哪个轴，那么就改成什么：这里是绕y轴 ---> transform.rotation.y
        
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
        //旋转角度
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI];
        //每次旋转的时间（单位秒）
        rotationAnimation.duration = 0.5;
        rotationAnimation.cumulative = YES;
        //重复旋转的次数，如果你想要无数次，那么设置成MAXFLOAT
        rotationAnimation.repeatCount = MAXFLOAT;
        
        [self.openButton.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
        WEAKSELF
        [WYHttpServer receiveRedPacketWithCoordinate:self.coordinate WithRedpacketId:self.redMessage.redpacketId WithRedpacketType:self.redMessage.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
            
            if (success) {
                //            WYRedPacketMessageController *redVc = [[WYRedPacketMessageController alloc]init];
                //            redVc.redPacket =self.redMessage;
                //            [weakSelf.navigationController pushViewController:redVc animated:YES];
                if (weakSelf.complte) {
                    [weakSelf playerVideo];
                    weakSelf.complte();
                   
                }
            }else{
                if (weakSelf.errorComplte) {
                    weakSelf.errorComplte(errorMessage);
                }
                
                //[weakSelf toast:errorMessage];
                //[weakSelf.view removeFromSuperview];
            }
        }];
        
        
        
        
        
        
        
        
}
}
void soundCompleteCallBack(SystemSoundID soundID, void *clientData)
{
    NSLog(@"播放完成");
}
-(void)playerVideo
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"openRedPacket" ofType:@"mp3"];
    NSURL *fileUrl = [NSURL URLWithString:filePath];
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallBack, NULL);
    
    AudioServicesPlaySystemSound(soundID);
}



-(void)dealloc
{
    
}


@end
